﻿using System;
using System.Collections.Generic;
using System.Linq;
using CSP.Common.Web;
using CSP.Common.Tools;
using CSP.Bll;
using CSP.Bll.DTO.ServiceInfo;
using CSP.Model.CSPEnum;
using CSP.Model.CSP;
using System.Text;
using System.Web;

namespace CSP.Web.UserControls
{
    public partial class SearchCondition : CSPUserControlBase
    {
        private const string REWRITERRULEPATH = "service/search.aspx?";

        private const string PARMSERVICETYPE = "servicetype";

        private const string PARMSERVICEAREA = "servicearea";

        private const string PARMSERVICEAGE = "serviceage";

        private string _currentPagePath = string.Empty;

        private string _requestServiceType = string.Empty;

        private string _requestServiceTypeSub = string.Empty;

        private string _requestServiceArea = string.Empty;

        private string _requestServiceAge = string.Empty;

        private string _requestServiceHouse = string.Empty;

        private string _requestServiceCar = string.Empty;
        private string _requestServiceTrust = string.Empty;

        private string _requestSearchValue = string.Empty;

        private bool _isAutoPostBack = true;

        private int _currentProvince;

        private StringBuilder _selectedTab = new StringBuilder();

        private string _selectCNContext = string.Empty;

        /// <summary>
        /// 当前请求的页面
        /// </summary>
        protected string CurrentPagePath
        {
            get
            {
                return _currentPagePath;
            }
        }

        /// <summary>
        /// 请求的服务类型
        /// </summary>
        protected string RequestServiceType
        {
            get
            {
                return _requestServiceType;
            }
        }

        protected string RequestServiceTypeSub
        {
            get
            {
                return _requestServiceTypeSub;
            }
        }

        /// <summary>
        /// 请求的服务地区
        /// </summary>
        protected string RequestServiceArea
        {
            get
            {
                return _requestServiceArea;
            }
        }

        /// <summary>
        /// 请求的年龄段
        /// </summary>
        protected string RequestServiceAge
        {
            get
            {
                return _requestServiceAge;
            }
        }

        protected string RequestServiceHouse
        {
            get
            {
                return _requestServiceHouse;
            }
        }

        protected string RequestServiceCar
        {
            get
            {
                return _requestServiceCar;
            }
        }
        protected string RequestServiceTrust
        {
            get
            {
                return _requestServiceTrust;
            }
        }

        /// <summary>
        /// 请求的结果值
        /// </summary>
        protected string RequestSearchValue
        {
            get
            {
                return _requestSearchValue;
            }
        }

        protected int CurrentProvince
        {
            get
            {
                return _currentProvince;
            }
        }

        /// <summary>
        /// 是否回发服务器默认true回发服务器 采用服务器回调方式 ，false不会发服务器采用JS函数回调方式
        /// </summary>
        public bool IsAutoPostBack
        {
            get
            {
                return _isAutoPostBack;
            }
            set
            {
                _isAutoPostBack = value;
            }
        }

        /// <summary>
        /// 选中的标签信息
        /// </summary>
        public string SelectedTab
        {
            get { return _selectedTab.ToString(); }
        }

        /// <summary>
        /// 搜索选中的中文名称
        /// </summary>
        public string SelectCNContext
        {
            get { return _selectCNContext; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitParm();
            if (Request["ajaxoperate"] != null && _requestServiceType == string.Empty && _requestServiceTypeSub == string.Empty && _requestServiceArea == string.Empty && _requestServiceAge == string.Empty && _requestSearchValue == string.Empty)
            {
                Response.Clear();
                Response.Write("{}");
                Response.End();
            }
            else if (Request["ajaxoperate"] != null)
            {
                Response.Clear();
                string serviceIdsJosn = GetServiceIdsJson();
                Response.Write(serviceIdsJosn);
                Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitParm();
            BindServiceTypeUI();
            BindServiceEreaUI();
            BindServiceAgeGroup();
            BindServiceHouse();
            BindServiceCar();
            BindServiceTrust();
            BindSearchValueUI();
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        private void InitParm()
        {
            _currentPagePath = this.Page.Request.Path;
            if (Request["servicetype"] != null)
            {
                _requestServiceType = Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                _requestServiceTypeSub = Request["servicesubId"];
            }

            if (Request["servicearea"] != null)
            {
                _requestServiceArea = Request["servicearea"].ToString();
            }
            if (Request["serviceage"] != null)
            {
                _requestServiceAge = Request["serviceage"].ToString();
            }
            if (Request["servicehouse"] != null)
            {
                _requestServiceHouse = Request["servicehouse"].ToString();
            }
            if (Request["servicecar"] != null)
            {
                _requestServiceCar = Request["servicecar"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                _requestServiceTrust = Request["servicetrust"].ToString();
            }
            if (Request["searchvalue"] != null)
            {
                _requestSearchValue = Request["searchvalue"].ToString();
            }

        }

        /// <summary>
        /// 返回ServiceId数组(提供服务器端调用)
        /// </summary>
        /// <returns></returns>
        [Obsolete("该方法暂时废弃")]
        public List<int> GetServiceIds()
        {
            return GetServiceIdsByParm();
        }

        /// <summary>
        /// 根据排序和分页参数获取信息
        /// </summary>
        /// <param name="orderCountSort"></param>
        /// <param name="serviceCreditSort"></param>
        /// <param name="priceSort"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public SearchServiceListResponseDTO GetSearchServiceList(bool? orderCountSort, bool? serviceCreditSort, bool? priceSort, int? pageIndex, int? pageSize)
        {
            SearchServiceListResponseDTO result = new SearchServiceListResponseDTO();
            //if (_requestServiceType == string.Empty && _requestServiceArea == string.Empty && _requestServiceAge == string.Empty && _requestServiceHouse ==string.Empty && _requestServiceCar==string.Empty && _requestServiceTrust==string.Empty && _requestSearchValue == string.Empty)
            //{
            //    return result;
            //}
            SearchServiceListRequestDTO request = GetReqeustByCondition(orderCountSort, serviceCreditSort, priceSort, pageIndex, pageSize);

            using (var serviceInfoBll = CSPBllFactory.GetInstance().GetServiceInfoBll())
            {
                result = serviceInfoBll.FindBySearchServiceList(request);
            }

            return result;
        }

        /// <summary>
        /// 返回ServiceId json字符串
        /// </summary>
        /// <returns></returns>
        private string GetServiceIdsJson()
        {
            List<int> ids = GetServiceIdsByParm();
            return CommonTool.SerializeObject(ids);
        }

        /// <summary>
        /// 获取ServiceIds
        /// </summary>
        /// <returns></returns>
        [Obsolete("该方法暂时废弃")]
        private List<int> GetServiceIdsByParm()
        {
            List<int> result = new List<int>();
            if (_requestServiceType == "" && _requestServiceArea == "" && _requestServiceAge == "" && _requestSearchValue == "")
            {
                return result;
            }
            FindIdsBySearchInfoRequestDTO request = GetRequestByCondition();

            using (var serviceInfoBll = CSPBllFactory.GetInstance().GetServiceInfoBll())
            {
                var ids = serviceInfoBll.FindIdsBySearchInfo(request);
                if (ids.Count != 0)
                {
                    result = ids;
                }
            }

            return result;
        }

        /// <summary>
        /// 绑定服务类型
        /// </summary>
        private void BindServiceTypeUI()
        {
            //List<ServiceType> serviceTypeAll = GetServiceTypes();
            //List<ServiceType> serviceDomain = serviceTypeAll.Where(s => s.Level == 2).ToList();
            //this.R_ServiceDomain.DataSource = serviceDomain;
            //this.R_ServiceDomain.DataBind();

            //ServiceType requestServiceType;
            //if (string.IsNullOrWhiteSpace(_requestServiceType) || (requestServiceType = serviceTypeAll.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceType))) == null)
            //{
            //    return;
            //}

            //int requestServiceTypeId;
            //if (requestServiceType.Level == 3)
            //{
            //    requestServiceTypeId = requestServiceType.ParentId;
            //}
            //else
            //{
            //    requestServiceTypeId = requestServiceType.Id;
            //}

            //List<ServiceType> serviceTypes = serviceTypeAll.Where(s => s.ParentId == requestServiceTypeId).ToList();
            //this.R_ServiceType.DataSource = serviceTypes;
            //this.R_ServiceType.DataBind();
            //ShowSelectedByNameAndType(requestServiceType.Name, 1);



            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 9);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == house.Parentid).ToList();
            this.R_ServiceDomain.DataSource = serviceData;
            this.R_ServiceDomain.DataBind();


            if (!string.IsNullOrWhiteSpace(_requestServiceType))
            {
                List<ServiceAsset> servicetypes = serviceAsset.Where(s => s.Parentid == Int32.Parse(_requestServiceType)).ToList();
                this.R_ServiceType.DataSource = servicetypes;
                this.R_ServiceType.DataBind();
                ServiceAsset requestHouseGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceType));

                if (requestHouseGroup != null)
                {
                    ShowSelectedByNameAndType(requestHouseGroup.Name, 1);
                }
                if (Request.QueryString["servicesubId"] != null)
                {
                    string _requestChildID = Request.QueryString["servicesubId"].ToString();
                    string[] childArray = _requestChildID.Split(',');
                    int childLen = childArray.Length;
                    if (childLen > 1) childLen--;
                    bool isFind = false;
                    for (int i = childLen; i >= 0; i--)
                    {
                        if (childArray[i] == string.Empty) continue;
                        //存在单选的情况
                        ServiceAsset requestChildGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(childArray[i].Split('-')[1]));
                        if (requestChildGroup != null)
                        {
                            ServiceAsset requestParentGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(requestChildGroup.Parentid));

                            if (requestParentGroup != null)
                            {
                                if (requestChildGroup.Level == 4)
                                {
                                    if (isFind == true) continue;
                                    isFind = true;
                                }
                                ShowSelectedByNameAndType(requestChildGroup.Name, 1, requestParentGroup.Name + "," + requestChildGroup.Id);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 绑定地区
        /// </summary>
        private void BindServiceEreaUI()
        {
            List<ServiceArea> serviceAreas = GetServiceArea();
            ServiceArea province = serviceAreas.FirstOrDefault(s => s.Name == "北京市");
            _currentProvince = province.Id;

            List<ServiceArea> serviceCity = serviceAreas.Where(s => s.Parentid == province.Id).ToList();
            this.R_AreaCity.DataSource = serviceCity;
            this.R_AreaCity.DataBind();

            ServiceArea requestServiceCity;
            if (string.IsNullOrWhiteSpace(_requestServiceArea) || (requestServiceCity = serviceAreas.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceArea))) == null)
            {
                return;
            }
            int requestServiceAreaId;
            if (requestServiceCity.Level == 3)
            {
                requestServiceAreaId = requestServiceCity.Parentid;
            }
            else
            {
                requestServiceAreaId = requestServiceCity.Id;
            }
            List<ServiceArea> serviceCounty = serviceAreas.Where(s => s.Parentid == requestServiceAreaId).ToList();
            this.R_AreaCounty.DataSource = serviceCounty;
            this.R_AreaCounty.DataBind();
            ShowSelectedByNameAndType(requestServiceCity.Name, 2);

        }
        private int childSelCount = 1;

        public int ChildSelCount
        {
            get { return childSelCount; }
            set { childSelCount = value; }
        }

        private int parentSelCount = 0;

        public int ParentSelCount
        {
            get { return parentSelCount; }
            set { parentSelCount = value; }
        }
        protected StringBuilder GetServiceChildUrl(string childId)
        {
            ParentSelCount++;
            string[] childArray = childId.Split(',');
            int childLen = childArray.Length;
            int _childId = 0;
            StringBuilder sb = new StringBuilder();
            StringBuilder childsb = new StringBuilder();
            for (int i = 0; i < childLen; i++)
            {
                string isFind = "none";
                int isfindId = 0;
                _childId = Int32.Parse(childArray[i]);
                //int _childId = Int32.Parse(childId);
                List<ServiceAsset> serviceAsset = GetAssetData();
                List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == _childId).ToList();


                string selectGroup = string.Empty;
                List<ServiceAsset> parentData = serviceAsset.Where(s => s.Id == _childId).ToList();
                {
                    foreach (ServiceAsset item in serviceData)
                    {
                        selectGroup += item.Id + ",";
                    }
                    sb.Append("<a href=\"#\" onclick=\"" + this.ClientID + ".DeleteChildRootClick('" + selectGroup + "')\" title=\"删除\">不限</a>");
                }
                foreach (ServiceAsset item in serviceData)
                {
                    if (item.Level == 999)
                    {
                        #region
                        //下拉显示,level为999处理　
                        //selectGroup = item.Level.ToString();

                        sb.Append("<select id='sel_" + ParentSelCount + "' onchange=\"selChange(this,this.options[this.options.selectedIndex].value)\">");
                        sb.Append("<option value='" + item.Id.ToString() + "'>" + item.Name + "</option>");
                        //获取下级
                        int startIndex = 1;
                        List<ServiceAsset> serviceSubProp = GetAssetData();
                        List<ServiceAsset> __serviceSubProp = serviceSubProp.Where(s => s.Parentid == item.Id).ToList();
                        foreach (ServiceAsset _subprop in __serviceSubProp)
                        {
                            sb.Append("<option value='" + startIndex + "'>" + _subprop.Name + "</option>");

                            List<ServiceAsset> serviceThirdProp = GetAssetData();
                            List<ServiceAsset> _serviceThirdProp = serviceSubProp.Where(s => s.Parentid == _subprop.Id).ToList();
                            if (_serviceThirdProp.Count > 0)
                            {
                                if (Request.QueryString["servicesubId"] != null)
                                {
                                    foreach (ServiceAsset __ThirdProp in _serviceThirdProp)
                                    {
                                        if (Request.QueryString["servicesubId"].ToString().IndexOf(__ThirdProp.Id.ToString()) > -1)
                                        {
                                            isFind = "true";
                                            isfindId = __ThirdProp.Id;
                                        }
                                    }
                                }
                                childsb.Append("<select id='subsel_" + ChildSelCount + "' name='subsel_" + ChildSelCount + "' onchange=\"selChildChange(this.options[this.options.selectedIndex].value)\"");
                                if (isFind == "none")
                                {
                                    childsb.Append(" style=\"display:none;\">");
                                }
                                else
                                {
                                    childsb.Append(">");
                                }

                                foreach (ServiceAsset __ThirdProp in _serviceThirdProp)
                                {
                                    childsb.Append("<option value='" + __ThirdProp.Id + "'");

                                    if (__ThirdProp.Id == isfindId)
                                    {
                                        childsb.Append(" selected=\"selected\">" + __ThirdProp.Name + "</option>");
                                    }
                                    else
                                    {
                                        childsb.Append(">" + __ThirdProp.Name + "</option>");
                                    }
                                }
                                isFind = "none";
                                childsb.Append("</select>");
                                startIndex++;
                                ChildSelCount++;
                            }
                            //sb.Append("<div class=\"ui-form-text\"><div class=\"text\"><input name='" + __name + "' type='text' value=\"3000\" /><span class=\"tail\">元</span></div></div>");
                            //startIndex++;
                        }
                        sb.Append("</select>");
                        if (childsb.Length > 0)
                        {
                            sb.Append(childsb.ToString());
                        }
                        #endregion
                    }
                    else if (item.Name == "OTHER")
                    {
                        string __name = "limit" + item.Id.ToString();
                        sb.Append("<div class=\"ui-form-text\"><div class=\"text\"><input name='" + __name + "' type='text' value=\"\" /><span class=\"tail\">元</span></div></div>");
                    }
                    else
                    {
                        //string[] _childMostArray = RequestServiceTypeSub.Split(',');
                        //int childMostLen = _childMostArray.Length;
                        //for (int j = 0; j < childMostLen; j++)
                        //{
                        //    if (_childMostArray[j] == string.Empty) continue;
                        //    if (_childMostArray[j] == item.Id.ToString())
                        //    {
                        //        sb.Append("<a class='select' disabled='true' style=\"cursor:Default;\" href=../" + GetServiceTypeUrl(item.Id.ToString(), item.Parentid.ToString()) + ">" + item.Name + "</a>");
                        //    }
                        //    else
                        //    {
                        //        sb.Append("<a href=../" + GetServiceTypeUrl(item.Id.ToString(), item.Parentid.ToString()) + ">" + item.Name + "</a>");
                        //        break;
                        //    }
                        //}                      
                        if (RequestServiceTypeSub.IndexOf("-"+item.Id.ToString() + "-") > -1)
                        {
                            sb.Append("<a class='select' disabled='true' style=\"cursor:Default;\" href=../" + GetServiceTypeUrl(item.Id.ToString(), item.Parentid.ToString()) + ">" + item.Name + "</a>");

                        }
                        else
                        {
                            sb.Append("<a href=../" + GetServiceTypeUrl(item.Id.ToString(), item.Parentid.ToString()) + ">" + item.Name + "</a>");
                        }

                    }
                }
            }
            return sb;
        }
        protected string GetServiceTypeUrl(string serviceTypeId, string serviceTypeParentId)
        {
            string result = REWRITERRULEPATH;
            string urlParm = string.Empty;
            bool isCurrentNode = false;
            string val = string.Empty;
            if (serviceTypeParentId != string.Empty)
            {
                List<ServiceAsset> serviceAsset = GetAssetData();


                ////找到所在父级
                List<ServiceAsset> valAsset = serviceAsset.Where(s => s.Id == Int32.Parse(serviceTypeId)).ToList();
                if (valAsset != null && valAsset.Count > 0)
                {
                    val = valAsset[0].val.ToString();
                }
                urlParm = PARMSERVICETYPE + "=" + RequestServiceType + "&servicesubId=" + serviceTypeParentId+"-"+ serviceTypeId+"-"+val + ",";
                if (Request.QueryString["servicesubId"] != null)
                {
                    string currentpara = Request.QueryString["servicesubId"];

                    string[] childArray = currentpara.Split(',');
                    int childLen = childArray.Length;
                    if (childLen > 1) childLen--;

                    for (int i = childLen; i >= 0; i--)
                    {
                        if (childArray[i] == string.Empty) continue;
                        //存在单选的情况
                        ServiceAsset requestChildGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(childArray[i].Split('-')[1]));
                        if (requestChildGroup != null)
                        {
                            if (requestChildGroup.Parentid.ToString() == serviceTypeParentId)
                            {

                            }
                            else
                            {
                                urlParm += childArray[i].ToString() + ",";
                            }
                            isCurrentNode = true;
                        }
                    }

                    if (isCurrentNode == false)
                    {
                        if (currentpara.IndexOf(serviceTypeId + ",") == -1)
                        {
                            //去掉重复
                            urlParm += Request.QueryString["servicesubId"].ToString() + ",";
                            urlParm = urlParm.Replace(",,", ",");

                        }
                        else
                        {
                            urlParm = PARMSERVICETYPE + "=" + RequestServiceType + "&servicesubId=" + currentpara;
                        }
                    }
                }
            }
            else
            {
                urlParm = PARMSERVICETYPE + "=" + serviceTypeId;
            }
            if (Request["servicearea"] != null)
            {
                urlParm += "&" + PARMSERVICEAREA + "=" + Request["servicearea"].ToString();
            }
            if (Request["serviceage"] != null)
            {
                urlParm += "&" + PARMSERVICEAGE + "=" + Request["serviceage"].ToString();
            }
            if (urlParm != "")
            {
                //urlParm += "/";
            }
            if (Request["servicehouse"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicehouse" + "=" + Request["servicehouse"].ToString();
            }
            if (Request["servicecar"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicecar" + "=" + Request["servicecar"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicetrust" + "=" + Request["servicetrust"].ToString();
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }

        /// <summary>
        /// 获取服务类型url
        /// </summary>
        /// <param name="serviceTypeId"></param>
        /// <returns></returns>
        protected string GetServiceTypeUrl(string serviceTypeId)
        {
            return GetServiceTypeUrl(serviceTypeId, string.Empty);

        }

        /// <summary>
        /// 获取服务地理位置url
        /// </summary>
        /// <param name="serviceAreaId"></param>
        /// <returns></returns>
        public string GetServiceAreaUrl(string serviceAreaId)
        {
            string result = REWRITERRULEPATH;
            string urlParm = "";
            if (Request["servicetype"] != null)
            {
                urlParm += PARMSERVICETYPE + "=" + Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicesubId=" + Request["servicesubId"].ToString();
            }

            if (urlParm != "")
            {
                urlParm += "&";
            }
            urlParm += PARMSERVICEAREA + "=" + serviceAreaId;
            if (Request["serviceage"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAGE + "=" + Request["serviceage"].ToString();
            }
            if (Request["servicehouse"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicehouse" + "=" + Request["servicehouse"].ToString();
            }
            if (Request["servicecar"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicecar" + "=" + Request["servicecar"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicetrust" + "=" + Request["servicetrust"].ToString();
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }

        /// <summary>
        /// 获取服务年龄
        /// </summary>
        /// <param name="serviceAge"></param>
        /// <returns></returns>
        public string GetServiceAgeUrl(string serviceAge)
        {
            string result = REWRITERRULEPATH;
            string urlParm = "";
            if (Request["servicetype"] != null)
            {
                urlParm += PARMSERVICETYPE + "=" + Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicesubId=" + Request["servicesubId"].ToString();
            }
            if (Request["servicearea"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAREA + "=" + Request["servicearea"].ToString();
            }
            if (Request["servicehouse"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicehouse" + "=" + Request["servicehouse"].ToString();
            }
            if (Request["servicecar"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicecar" + "=" + Request["servicecar"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicetrust" + "=" + Request["servicetrust"].ToString();
            }
            if (urlParm != "")
            {
                urlParm += "&";
            }
            urlParm += PARMSERVICEAGE + "=" + serviceAge;

            if (urlParm != "")
            {
                //urlParm += "/";
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }

        /// <summary>
        /// 获取房产信息
        /// </summary>
        /// <param name="serviceAge"></param>
        /// <returns></returns>
        public string GetServiceHouseUrl(string serviceHouse)
        {
            string result = REWRITERRULEPATH;
            string urlParm = "";
            if (Request["servicetype"] != null)
            {
                urlParm += PARMSERVICETYPE + "=" + Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicesubId=" + Request["servicesubId"].ToString();
            }
            if (Request["servicearea"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAREA + "=" + Request["servicearea"].ToString();
            }
            if (Request["serviceage"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAGE + "=" + Request["serviceage"].ToString();
            }
            urlParm += "&servicehouse" + "=" + serviceHouse;

            if (urlParm != "")
            {
                //urlParm += "/";
            }
            if (Request["servicecar"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicecar" + "=" + Request["servicecar"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicetrust" + "=" + Request["servicetrust"].ToString();
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }

        public string GetServiceCarUrl(string serviceCar)
        {
            string result = REWRITERRULEPATH;
            string urlParm = "";
            if (Request["servicetype"] != null)
            {
                urlParm += PARMSERVICETYPE + "=" + Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicesubId=" + Request["servicesubId"].ToString();
            }
            if (Request["servicearea"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAREA + "=" + Request["servicearea"].ToString();
            }

            if (Request["serviceage"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAGE + "=" + Request["serviceage"].ToString();
            }
            if (Request["servicehouse"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicehouse" + "=" + Request["servicehouse"].ToString();
            }
            if (Request["servicetrust"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicetrust" + "=" + Request["servicetrust"].ToString();
            }
            urlParm += "&servicecar" + "=" + serviceCar;

            if (urlParm != "")
            {
                //urlParm += "/";
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }
        public string GetServiceTrustUrl(string serviceTrust)
        {
            string result = REWRITERRULEPATH;
            string urlParm = "";
            if (Request["servicetype"] != null)
            {
                urlParm += PARMSERVICETYPE + "=" + Request["servicetype"].ToString();
            }
            if (Request["servicesubId"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicesubId=" + Request["servicesubId"].ToString();
            }
            if (Request["servicearea"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAREA + "=" + Request["servicearea"].ToString();
            }
            if (Request["serviceage"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += PARMSERVICEAGE + "=" + Request["serviceage"].ToString();
            }
            if (Request["servicehouse"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicehouse" + "=" + Request["servicehouse"].ToString();
            }
            if (Request["servicecar"] != null)
            {
                if (urlParm != "")
                {
                    urlParm += "&";
                }
                urlParm += "servicecar" + "=" + Request["servicecar"].ToString();
            }
            urlParm += "&serviceTrust" + "=" + serviceTrust;

            if (urlParm != "")
            {
                //urlParm += "/";
            }
            if (Request["searchvalue"] != null)
            {
                urlParm += "&" + "searchvalue=" + Request["searchvalue"].ToString();
            }

            result += urlParm;
            return result;
        }
        /// <summary>
        /// 绑定年龄组
        /// </summary>
        public void BindServiceAgeGroup()
        {
            List<ServiceAsset> ageGroups = GetAssetData();
            //ServiceAsset age = ageGroups.FirstOrDefault(s => s.Parentid == 7);
            List<ServiceAsset> serviceData = ageGroups.Where(s => s.Parentid == 7).ToList();
            this.R_AgeGroup.DataSource = serviceData;
            this.R_AgeGroup.DataBind();
            if (!string.IsNullOrWhiteSpace(_requestServiceAge))
            {
                ServiceAsset requestAgeGroup = ageGroups.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceAge));
                ShowSelectedByNameAndType(requestAgeGroup.Name, 3);
            }
        }

        public void BindServiceHouse()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            //ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 1);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == 1).ToList();
            this.R_House.DataSource = serviceData;
            this.R_House.DataBind();
            if (!string.IsNullOrWhiteSpace(_requestServiceHouse))
            {
                ServiceAsset requestHouseGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceHouse));
                ShowSelectedByNameAndType(requestHouseGroup.Name, 4);
            }
        }
        public void BindServiceCar()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            //ServiceAsset car = serviceAsset.FirstOrDefault(s => s.Parentid == 2);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == 2).ToList();
            this.R_Car.DataSource = serviceData;
            this.R_Car.DataBind();
            if (!string.IsNullOrWhiteSpace(_requestServiceCar))
            {
                ServiceAsset requestCarGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceCar));
                ShowSelectedByNameAndType(requestCarGroup.Name, 5);
            }
        }

        public void BindServiceTrust()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            //ServiceAsset trust = serviceAsset.FirstOrDefault(s => s.Parentid == 3);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == 3).ToList();
            this.R_Trust.DataSource = serviceData;
            this.R_Trust.DataBind();
            if (!string.IsNullOrWhiteSpace(_requestServiceTrust))
            {
                ServiceAsset requestTrustGroup = serviceAsset.FirstOrDefault(s => s.Id == Convert.ToInt32(_requestServiceTrust));
                ShowSelectedByNameAndType(requestTrustGroup.Name, 6);
            }
        }

        private void BindSearchValueUI()
        {
            if (!string.IsNullOrWhiteSpace(_requestSearchValue))
            {
                ShowSelectedByNameAndType(_requestSearchValue, 7);
                Txt_SearchValue.Value = _requestSearchValue;
            }
        }

        /// <summary>
        /// 根据条件获得 用于查询的请求参数
        /// </summary>
        /// <returns></returns>
        [Obsolete("该方法暂时废弃")]
        private FindIdsBySearchInfoRequestDTO GetRequestByCondition()
        {
            FindIdsBySearchInfoRequestDTO request = new FindIdsBySearchInfoRequestDTO();
            if (_requestServiceType != "")
            {
                request.ServiceType = Convert.ToInt32(_requestServiceType);
            }

            if (_requestServiceArea != "")
            {
                request.Area = Convert.ToInt32(_requestServiceArea);
                request.AreaLeave = GetAreaLeaveByServiceAreaId(request.Area.Value);
            }

            if (_requestServiceAge != "")
            {
                request.AgeGroupType = _requestServiceAge;
            }

            if (_requestSearchValue != "")
            {
                if (_requestSearchValue.Length < 20)
                {
                    request.SearchValue = _requestSearchValue;
                }
                else
                {
                    request.SearchValue = _requestSearchValue.Substring(0, 20);
                }

            }
            return request;
        }

        private SearchServiceListRequestDTO GetReqeustByCondition(bool? orderCountSort, bool? serviceCreditSort, bool? priceSort, int? pageIndex, int? pageSize)
        {
            SearchServiceListRequestDTO request = new SearchServiceListRequestDTO();
            request.OrderCountSort = orderCountSort;
            request.ServiceCreditSort = serviceCreditSort;
            request.PriceSort = priceSort;
            request.PageIndex = pageIndex;
            request.PageSize = pageSize;

            if (_requestServiceType != "")
            {
                request.ServiceTypeId = Convert.ToInt32(_requestServiceType);
            }

            if (_requestServiceArea != "")
            {
                int area = Convert.ToInt32(_requestServiceArea);
                int areaLeve = GetAreaLeaveByServiceAreaId(area);
                //省份
                if (areaLeve == 1)
                {
                    request.Province = area;
                }
                //城市
                else if (areaLeve == 2)
                {
                    request.City = area;
                }
                //地区
                else if (areaLeve == 3)
                {
                    request.County = area;
                }
            }

            if (_requestServiceAge != "")
            {
                request.AgeStart = _requestServiceAge;
                //AgeGroupType ageGroupType = (AgeGroupType)Convert.ToInt32(_requestServiceAge);
                //if(ageGroupType==AgeGroupType.OneToThree)
                //{
                //    request.AgeStart = 20;
                //    request.AgeEnd = 25;
                //}
                //else if (ageGroupType == AgeGroupType.ThreeToSix)
                //{
                //    request.AgeStart = 25;
                //    request.AgeEnd = 30;
                //}
                //else if (ageGroupType == AgeGroupType.SixToNine)
                //{
                //    request.AgeStart = 30;
                //    request.AgeEnd = 35;
                //}
                //else if (ageGroupType == AgeGroupType.NineToTwelve)
                //{
                //    request.AgeStart = 35;
                //    request.AgeEnd = 40;
                //}
                //else if (ageGroupType == AgeGroupType.TwelveToFifteen)
                //{
                //    request.AgeStart = 40;
                //    request.AgeEnd = 45;
                //}
                //else if (ageGroupType == AgeGroupType.OverFifteen)
                //{
                //    request.AgeStart = 45;
                //    request.AgeEnd = 50;
                //}
                //else if (ageGroupType == AgeGroupType.OverSixteen)
                //{
                //    request.AgeStart = 50;
                //    request.AgeEnd = 55;
                //}
            }
            if (_requestServiceHouse != string.Empty)
            {
                request.HouseCase = _requestServiceHouse;
            }
            if (_requestServiceCar != string.Empty)
            {
                request.CarCase = _requestServiceCar;
            }
            if (_requestServiceTrust != string.Empty)
            {
                request.TrustCase = _requestServiceTrust;
            }

            if (_requestSearchValue != "")
            {
                if (_requestSearchValue.Length < 20)
                {
                    request.SearchValue = _requestSearchValue;
                }
                else
                {
                    request.SearchValue = _requestSearchValue.Substring(0, 20);
                }

            }
            if (_requestServiceTypeSub != "")
            {
                string[] splitArray = _requestServiceTypeSub.Split(',');
                int splitArrayLen = splitArray.Length;
                var currentId = string.Empty;
                for (int i = 0; i < splitArrayLen; i++)
                {
                    if (string.IsNullOrEmpty(splitArray[i])) continue;
                    currentId = splitArray[i];
                    if (currentId.StartsWith("000"))
                    {
                        currentId = currentId.Replace("000", "") + ",";
                    }
                    else
                    {
                        currentId = currentId + ",";
                    }

                    request.ServiceTypeSubId += currentId;
                }
            }
            return request;
        }

        /// <summary>
        /// 根据地区Id 获取地区的级别
        /// </summary>
        /// <param name="serviceAreaId"></param>
        /// <returns></returns>
        private int GetAreaLeaveByServiceAreaId(int serviceAreaId)
        {
            int result;
            using (var serviceAreaBll = CSPBllFactory.GetInstance().GetServiceAreaBll())
            {
                result = serviceAreaBll.GetDataById(serviceAreaId).Level;
            }

            return result;
        }


        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceType> GetServiceTypes()
        {
            List<CSP.Model.CSP.ServiceType> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceTypeBll())
            {
                result = serviceTypeBll.FindAll().ToList();
            }
            return result;
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceArea> GetServiceArea()
        {
            List<CSP.Model.CSP.ServiceArea> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceAreaBll())
            {
                result = serviceTypeBll.FindAll().ToList();
            }
            return result;
        }

        /// <summary>
        /// 获取年龄组列表
        /// </summary>
        /// <returns></returns>
        private List<AgeGroup> GetAgeGroups()
        {
            List<AgeGroup> result = new List<AgeGroup>();
            AgeGroup groupOneToThree = new AgeGroup();
            groupOneToThree.Id = Convert.ToInt32(AgeGroupType.OneToThree);
            groupOneToThree.Name = "20-25岁";

            AgeGroup groupThreeToSix = new AgeGroup();
            groupThreeToSix.Id = Convert.ToInt32(AgeGroupType.ThreeToSix);
            groupThreeToSix.Name = "25-30岁";

            AgeGroup groupSixToNine = new AgeGroup();
            groupSixToNine.Id = Convert.ToInt32(AgeGroupType.SixToNine);
            groupSixToNine.Name = "30-35岁";

            AgeGroup groupNineToTwelve = new AgeGroup();
            groupNineToTwelve.Id = Convert.ToInt32(AgeGroupType.NineToTwelve);
            groupNineToTwelve.Name = "35-40岁";

            AgeGroup groupTwelveToFifteen = new AgeGroup();
            groupTwelveToFifteen.Id = Convert.ToInt32(AgeGroupType.TwelveToFifteen);
            groupTwelveToFifteen.Name = "40-45岁";

            AgeGroup groupOverFifteen = new AgeGroup();
            groupOverFifteen.Id = Convert.ToInt32(AgeGroupType.OverFifteen);
            groupOverFifteen.Name = "45-50岁";

            AgeGroup groupOverSixteen = new AgeGroup();
            groupOverSixteen.Id = Convert.ToInt32(AgeGroupType.OverSixteen);
            groupOverSixteen.Name = "50-55岁";

            result.Add(groupOneToThree);
            result.Add(groupThreeToSix);
            result.Add(groupSixToNine);
            result.Add(groupNineToTwelve);
            result.Add(groupTwelveToFifteen);
            result.Add(groupOverFifteen);
            result.Add(groupOverSixteen);


            return result;
        }
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;

            return objCache[CacheKey];
        }

        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }
        private List<CSP.Model.CSP.ServiceAsset> GetAssetData()
        {
            //List<AgeGroup> result = new List<AgeGroup>();
            //return result;
            if (GetCache("AssetData") != null)
            {
                return GetCache("AssetData") as List<CSP.Model.CSP.ServiceAsset>;
            }
            List<CSP.Model.CSP.ServiceAsset> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceAssetBll())
            {
                result = serviceTypeBll.FindAll().ToList();

                SetCache("AssetData", result);
            }
            return result;
        }
        private void ShowSelectedByNameAndType(string name, int type)
        {
            ShowSelectedByNameAndType(name, type, string.Empty);
        }
        /// <summary>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        private void ShowSelectedByNameAndType(string name, int type, string parentName)
        {
            if (type == 1)
            {
                _selectCNContext += name + " ";

                if (parentName == string.Empty)
                {
                    _selectedTab.Append("<span>服务类型：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteTypeClick()\" title=\"删除\"></b></a>");

                }
                else
                {
                    string[] a = parentName.Split(',');
                    _selectedTab.Append("<span>" + a[0].ToString() + "：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteSubClick('" + a[1].ToString() + "')\" title=\"删除\"></b></a>");

                }
            }
            else if (type == 2)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>服务区域：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteAreaClick()\" title=\"删除\"></b></a>");
            }
            else if (type == 3)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>年龄段：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteAgeClick()\" title=\"删除\"></b></a>");
            }
            else if (type == 4)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>名下房产：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteHouseClick()\" title=\"删除\"></b></a>");
            }
            else if (type == 5)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>名下车辆：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteCarClick()\" title=\"删除\"></b></a>");
            }
            else if (type == 6)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>个人信用：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteTrustClick()\" title=\"删除\"></b></a>");
            }
            else if (type == 7)
            {
                _selectCNContext += name + " ";
                _selectedTab.Append("<span>搜索结果：</span> <a href=\"#\">" + name + "<b class=\"del\" onclick=\"" + this.ClientID + ".DeleteSearchValueClick()\" title=\"删除\"></b></a>");
            }
        }

        private class AgeGroup
        {
            public int Id
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }
        }
    }
}